<?php
namespace Admin\Controller;

use Think\Controller;

class LbPictureController extends CommonController
{
    public function index()
    {
        $picture = M('lb_picture')->select();
        $this->assign('picture', $picture);
        $this->display();
    }

    //显示轮播图
    public function edit()
    {
        $id = I('id');
        if (M('LbPicture')->where('id=' . $id)->save(array('status' => 1))) {
            $this->success('已在首页显示');
        } else {
            $this->error('数据异常');
        }
    }

    public function upload()
    {
        $type = $_FILES['img']['type'];
        // 判断类型
        if (strstr($type, 'image/jpeg')) {
            $ext = '.jpg';
        } elseif (strstr($type, 'image/png')) {
            $ext = '.png';
        } elseif (strstr($type, 'image/gif')) {
            $ext = '.gif';
        } else {
            echo '不支持此类型文件';
            exit();
        }
        //生成文件名
        $name = time() . rand(100, 999) . $ext;
        //保存图片路径
        $path = './Upload/' . $name;
        if (move_uploaded_file($_FILES['img']['tmp_name'], $path)) {
            echo ltrim($path,'.');
        } else {
            echo 0;
        }
    }

    //添加图片
    public function doadd()
    {
        if (M('LbPicture')->add(array('lb_picture' => I('imgurl')))) {
            $this->ajaxReturn(array('info' => '添加成功', 'status' => 1));
        } else {
            $this->ajaxReturn(array('info' => '添加失败', 'status' => 0));
        }
    }

    //删除
    public function del()
    {

        $uids = isset($_REQUEST['uids']) ? $_REQUEST['uids'] : false;
        if (is_array($uids)) {
            foreach ($uids as $k => $v) {
                $uids[$k] = intval($v);
            }
            if (!$uids) {
                $this->error('参数错误！');
                $uids = implode(',', $uids);
            }
        }
        $where['id'] = array('in', $uids);
        if(M('LbPicture')->where($where)->delete()){
            $this->success('删除成功');
        }else{
            $this->error('操作异常');
        }
    }
}